package com.ruoyi.youtian.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.youtian.domain.vo.ScenePointVo;
import com.ruoyi.youtian.service.ITEarlyWarningService;
import com.ruoyi.youtian.domain.vo.TEarlyWarningVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.youtian.mapper.TEarlyWarningMapper;
import com.ruoyi.youtian.domain.TEarlyWarning;
import com.ruoyi.common.core.text.Convert;

/**
 * 预警设置Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-12
 */
@Service
public class TEarlyWarningServiceImpl implements ITEarlyWarningService
{
    @Autowired
    private TEarlyWarningMapper tEarlyWarningMapper;

    /**
     * 查询预警设置
     * 
     * @param id 预警设置主键
     * @return 预警设置
     */
    @Override
    public TEarlyWarning selectTEarlyWarningById(Long id)
    {
        return tEarlyWarningMapper.selectTEarlyWarningById(id);
    }

    /**
     * 查询预警设置列表
     * 
     * @param tEarlyWarning 预警设置
     * @return 预警设置
     */
    @Override
    public List<TEarlyWarning> selectTEarlyWarningList(TEarlyWarning tEarlyWarning)
    {
        return tEarlyWarningMapper.selectTEarlyWarningList(tEarlyWarning);
    }

    /**
     * 新增预警设置
     * 
     * @param tEarlyWarning 预警设置
     * @return 结果
     */
    @Override
    public int insertTEarlyWarning(TEarlyWarning tEarlyWarning)
    {
        tEarlyWarning.setCreateTime(DateUtils.getNowDate());
        return tEarlyWarningMapper.insertTEarlyWarning(tEarlyWarning);
    }


    /**
     * 新增预警设置（功能修改）
     *
     * @param tEarlyWarning 预警设置
     * @return 结果
     */
    @Override
    public int reinsertTEarlyWarning(TEarlyWarning tEarlyWarning)
    {
        if (tEarlyWarning.getSourceTag() != null && tEarlyWarning.getDescription() != null) {
            tEarlyWarning.setCreateTime(DateUtils.getNowDate());
            return tEarlyWarningMapper.reinsertTEarlyWarning(tEarlyWarning);
        }
        tEarlyWarning.setCreateTime(DateUtils.getNowDate());
        tEarlyWarningMapper.reinsertTEarlyWarning(tEarlyWarning);

//        List<ScenePointVo> list = tEarlyWarningMapper.reselectWarning(tEarlyWarning.getSceneId());
//        for (ScenePointVo scenePointVo : list) {
//            tEarlyWarning.setDescription(scenePointVo.getPointName());
//            tEarlyWarning.setSourceTag(scenePointVo.getPointId());
//            tEarlyWarningMapper.reinsertTEarlyWarning(tEarlyWarning);
//        }
        return 1;
    }



    /**
     * 修改预警设置
     * 
     * @param tEarlyWarning 预警设置
     * @return 结果
     */
    @Override
    public int updateTEarlyWarning(TEarlyWarning tEarlyWarning)
    {
        return tEarlyWarningMapper.updateTEarlyWarning(tEarlyWarning);
    }

    /**
     * 批量删除预警设置
     * 
     * @param ids 需要删除的预警设置主键
     * @return 结果
     */
    @Override
    public int deleteTEarlyWarningByIds(String ids)
    {
        return tEarlyWarningMapper.deleteTEarlyWarningByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除预警设置信息
     * 
     * @param id 预警设置主键
     * @return 结果
     */
    @Override
    public int deleteTEarlyWarningById(Long id)
    {
        return tEarlyWarningMapper.deleteTEarlyWarningById(id);
    }

    /**
     * 预警设置列表
     * @param vo
     * @return
     */
    @Override
    public List<TEarlyWarningVo> selectEarlyWarningVoList(TEarlyWarningVo vo) {
        return this.tEarlyWarningMapper.selectEarlyWarningVoList(vo);
    }

    @Override
    public int changeEarlyWaringStatus(String ids, String status) {
        TEarlyWarning bean = new TEarlyWarning();
        int res = 0;
        String[] idArray = Convert.toStrArray(ids);
        for (String id: idArray) {
            bean.setId(Long.parseLong(id));
            bean.setStatus(status);
            int i = this.tEarlyWarningMapper.updateTEarlyWarning(bean);
            res = res + i;
            // Todo 根据 status的值去 更新 分析线程的状态
        }
        return res;
    }
}
